package com.bracelet.controller;

import com.bracelet.Enum.SeviceId;
import com.bracelet.service.impl.SaveMessageFormatServiceImp;
import com.bracelet.util.netty.NettySession;
import com.bracelet.util.netty.NettySessionManager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
* 接收手环设备信息处理Controller
* */
@Slf4j
@RestController
public class MessageFormatController {

    @Autowired
    SaveMessageFormatServiceImp saveMessageFormatServiceImp;


    // 链路保持
    @RequestMapping(value = "/Linkhold")
    public ObjectNode Linkhold(@RequestBody String args){
        ObjectNode returnMessage = null;
        // 链路保持仅需正常响应固定字符串 [DW*设备ID*0002*KA]
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String args1 = jsonNode.get("args").asText().replaceAll("\\[|\\]", "");
            String[] titleMessage = args1.split("[*]");
            returnMessage = mapper.createObjectNode();
            returnMessage.put("args","[DW*"+titleMessage[1]+"*"+titleMessage[1]+"*KA]");
            log.info("设备{}链路保持：{}",titleMessage[1],returnMessage);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }


    // 位置数据上报接口  无需响应
    @RequestMapping(value = "/SaveMessageFormat")
    public ObjectNode SaveMessageFormat(@RequestBody String args){
        ObjectNode returnMessage = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String args1 = jsonNode.get("args").asText().replaceAll("\\[|\\]", "").replaceAll("[*]", ",");
            String[] titleMessage = args1.split(",");
            log.info("设备{}发送指令:{},数据信息：{}",titleMessage[3], SeviceId.getName(titleMessage[3]),args1);
            String aClass = SeviceId.getClass(titleMessage[3]);
           if(!StringUtils.isEmpty(aClass)){
               saveMessageFormatServiceImp.exec(SeviceId.getClass(titleMessage[3]),args1);
           }else{
               log.info("交易码{}未识别，请确认是否需要匹配指定交易！",titleMessage[3]);
           }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }

    //报警数据 仅涉及电量 是否佩戴 跌倒 意义不大 暂时不做处理  固定返回[DW*YYYYYYYYYYYYYYY*LEN*AL]
    @RequestMapping(value = "/DataAlert")
    public ObjectNode DataAlert(@RequestBody String args){
        ObjectNode returnMessage = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String args1 = jsonNode.get("args").asText().replaceAll("\\[|\\]", "");
            String[] titleMessage = args1.split("[*]");
            returnMessage = mapper.createObjectNode();
            returnMessage.put("args","[DW*"+titleMessage[1]+"*LEN*AL]");
            log.info("设备{}告警：{}",titleMessage[1],returnMessage);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }

    //获取天气 反显手表 不重要 后边想加可以上
    @RequestMapping(value = "/Weather")
    public ObjectNode Weather(@RequestBody String args){
        ObjectNode returnMessage = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String args1 = jsonNode.get("args").asText().replaceAll("\\[|\\]", "");
            String[] titleMessage = args1.split("[*]");
            returnMessage = mapper.createObjectNode();
            returnMessage.put("args","[DW*"+titleMessage[1]+"*"+titleMessage[1]+"*WT]");
            log.info("设备{}获取天气：{}",titleMessage[1],returnMessage);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }

    //请求位置数据 根据手机上送经纬度解析后返回地址  这个解析需要成本 此处不开发
    @RequestMapping(value = "/LocationRequest")
    public ObjectNode LocationRequest(@RequestBody String args){
        ObjectNode returnMessage = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String args1 = jsonNode.get("args").asText().replaceAll("\\[|\\]", "");
            String[] titleMessage = args1.split("[*]");
            returnMessage = mapper.createObjectNode();
            returnMessage.put("args","[DW*"+titleMessage[1]+"*"+titleMessage[1]+"*WG]");
            log.info("设备{}获取位置：{}",titleMessage[1],returnMessage);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }

    //设备开机后主动上报 1 次设备 iccid
    // 由于目前无法捕获关机动作 无法针对该接口获取有意义信息 故暂时不处理
    @RequestMapping(value = "/ICCIDReported")
    public ObjectNode ICCIDReported(@RequestBody String args){
        ObjectNode returnMessage = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String args1 = jsonNode.get("args").asText().replaceAll("\\[|\\]", "");
            String[] titleMessage = args1.split("[*]");
            returnMessage = mapper.createObjectNode();
            returnMessage.put("args","[DW*"+titleMessage[1]+"*"+titleMessage[1]+"*ICCID]");
            log.info("设备{}ICCID上报：{}",titleMessage[1],returnMessage);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }


    // 终端心率上传
    @RequestMapping(value = "/heart")
    public ObjectNode heart(@RequestBody String args){
        ObjectNode returnMessage = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String info = jsonNode.get("args").asText().replaceAll("\\[|\\]", "").replaceAll("[*]", ",");
            String className = jsonNode.get("className").asText();
            returnMessage = saveMessageFormatServiceImp.heart(className,info);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }

    // 终端体温上传
    @RequestMapping(value = "/temp")
    public ObjectNode temp(@RequestBody String args){
        ObjectNode returnMessage = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String info = jsonNode.get("args").asText().replaceAll("\\[|\\]", "").replaceAll("[*]", ",");
            String className = jsonNode.get("className").asText();
            returnMessage = saveMessageFormatServiceImp.temp(className,info);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }
    // 终端血压上传
    @RequestMapping(value = "/blood")
    public ObjectNode blood(@RequestBody String args){
        ObjectNode returnMessage = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String info = jsonNode.get("args").asText().replaceAll("\\[|\\]", "").replaceAll("[*]", ",");
            String className = jsonNode.get("className").asText();
            returnMessage = saveMessageFormatServiceImp.blood(className,info);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }
    // 终端血氧上传
    @RequestMapping(value = "/oxygen")
    public ObjectNode oxygen(@RequestBody String args){
        ObjectNode returnMessage = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = mapper.readValue(args, JsonNode.class);
            String info = jsonNode.get("args").asText().replaceAll("\\[|\\]", "").replaceAll("[*]", ",");
            String className = jsonNode.get("className").asText();
            returnMessage = saveMessageFormatServiceImp.oxygen(className,info);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }

    @RequestMapping(value = "/closeed")
    public ObjectNode closeed(){
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode returnMessage = mapper.createObjectNode();
        Map<String, NettySession> NettySessionmMaps = NettySessionManager.getSessionMap();
        Set<String> strings = NettySessionmMaps.keySet();
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            NettySession nettySession = NettySessionmMaps.get(next);
            nettySession.getChannel().writeAndFlush("[DW*"+nettySession.getBizKey()+"*0004*"+"POWEROFF"+"]");
            log.info("===="+"[DW*"+nettySession.getBizKey()+"*0004*"+"POWEROFF"+"]");
        }
        return returnMessage;
    }

}
